package myzzyl.service.Impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import myzzyl.constants.CommonStrField;
import myzzyl.constants.ErrorConstants;
import myzzyl.domain.dto.MemberLoginDto;
import myzzyl.domain.pojo.Member;
import myzzyl.domain.vo.MemberLoginVo;
import myzzyl.exception.BusinessException;
import myzzyl.properties.JwtProperties;
import myzzyl.response.BaseResponse;
import myzzyl.service.MemberService;
import myzzyl.mapper.MemberMapper;
import myzzyl.utils.JwtUtil;
import myzzyl.utils.WxServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.sql.Struct;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
* @author 33249
* @description 针对表【member】的数据库操作Service实现
* @createDate 2025-08-12 10:04:50
*/
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member>
    implements MemberService{


    @Autowired
    private WxServiceUtil wxServiceUtil;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 小程序端用户wx登录
     * @param memberLoginDto 登录表单请求封装类
     * @return 返回值
     */
    @Override
    public MemberLoginVo wxLogin(MemberLoginDto memberLoginDto) {
        // 调用微信登录接口
        String openId = wxServiceUtil.login(memberLoginDto.getCode());

        // 调用获取手机号接口
        String phone = wxServiceUtil.getPhone(memberLoginDto.getPhoneCode());

        // 根据openId查看用户是否是第一次登陆
        Member member = lambdaQuery().eq(Member::getOpenId, openId).one();
        if(member == null) {
            // 第一次登陆的话自动注册
            // 生成随机用户名
            String nickName = "nick" + IdUtil.fastSimpleUUID().substring(16);
            member = new Member();
            member.setName(nickName);
            member.setOpenId(openId);
            member.setPhone(phone);

            save(member);
        }else {
            // 否则，判断用户绑定的手机号是否更换
            if(!ObjectUtil.equal(memberLoginDto.getNickName(), member.getName())) {
                lambdaUpdate()
                        .set(Member::getPhone, phone)
                        .eq(Member::getId, member.getId())
                        .update();
            }
        }

        // 构造声明map
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", member.getId());
        map.put("name", member.getName());
        map.put("avatar", member.getAvatar());
        map.put("openId", member.getOpenId());

        // 生成jwt
        String jwt = JwtUtil.generateJwt(jwtProperties.getSecret(), map, jwtProperties.getExpirationTime());
        // 存入redis
        redisTemplate.opsForValue().set(CommonStrField.USER_TOKEN.getStr() + member.getId(), jwt,
                jwtProperties.getExpirationTime(), TimeUnit.MILLISECONDS);

        // 封装Vo对象返回
        return new MemberLoginVo(jwt, member.getName());
    }



}




